/**
 * @Title:  AttentionDynamicsActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2015-12-7 上午10:20:14 
 * @version:  V1.0 
 */

package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.AuctionFragmentAdapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.fragment.LocalFragment;
import artid.com.arttogoa.fragment.SDFragment;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.view.SubViewPager;

/**
 * TODO<主体页面>
 * 
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class HomeActivity extends FragmentActivity {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */

	private HorizontalScrollView mTabScroll;
	private LinearLayout mTabs;
	private RelativeLayout notMac;
	private int mTabCount;

	private SubViewPager mViewPager;
	// 主适配器
	private AuctionFragmentAdapter mFragmentAdapter;

	private ImageView mCursorView;
	private int mOffset, mCurrIndex, mScreenWidth;

	private int tabSelection = 0;
	private static final String TAG = "Browse";
	// private Bundle mArgs;
	private LinearLayout lay;
	float x = 0;
	float y = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_sum);
		mCursorView = (ImageView) findViewById(R.id.cursor_view);
		mViewPager = (SubViewPager) findViewById(R.id.viewPager);
		mTabScroll = (HorizontalScrollView) findViewById(R.id.layout_scroll);
		notMac = (RelativeLayout) findViewById(R.id.rel_not_mic);
		lay = (LinearLayout) findViewById(R.id.ll_lay);
		mTabs = (LinearLayout) findViewById(R.id.tabs);
		ImageView back = (ImageView) findViewById(R.id.title_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		mScreenWidth = displayMetrics.widthPixels;
		// if (AppUtil.getMainList() == null) {
		// JumpUtil.main(getActivity());
		// return;
		// }
		mTabCount = 2;
		mFragmentAdapter = new AuctionFragmentAdapter(
				getSupportFragmentManager());

		type = getIntent().getStringExtra("type");

		if (!TextUtils.isEmpty(type) && type.equals(Cache.T_COM)) {
			x = (this.getWindowManager().getDefaultDisplay().getWidth());
			y = Integer.valueOf(MySharedPreferences.getMySharedPreferences(
					HomeActivity.this).getValue(Cache.ART_SIZE, ""));
		}
		List<Fragment> mList = new ArrayList<Fragment>();
		SDFragment sd = new SDFragment(x, y);
		LocalFragment lf = new LocalFragment(x, y);
		mList.add(sd);
		mList.add(lf);
		mFragmentAdapter.setData(mList);
		mFragmentAdapter.notifyDataSetChanged();
		mViewPager.setAdapter(mFragmentAdapter);
		mViewPager.setOffscreenPageLimit(2);
		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		initTabs();
		if (TextUtils.isEmpty(type)) {
			mViewPager.setCurrentItem(1);
		} else if (type.equals(Cache.T_COM) || type.equals(Cache.T_GP)) {
			mViewPager.setCurrentItem(1);
			notMac.setVisibility(View.GONE);
		} else if (type.equals(Cache.T_DIV)) {
			mViewPager.setCurrentItem(0);
			notMac.setVisibility(View.GONE);
		} else if (type.equals(Cache.T_PH)) {
			int pictureLimit = getIntent().getIntExtra("pictureLimit", 0);
			mViewPager.setCurrentItem(getIntent().getIntExtra("index", 0));
			if (pictureLimit != 0)
				notMac.setVisibility(View.GONE);
			tabSelection =   getIntent().getIntExtra("index", 0);
			System.out.println("index=" + getIntent().getIntExtra("index", 0));
		}

	}

	private String type;

	private void initTabs() {
		// int width = (int) (mScreenWidth / 4.0 + 0.5f);
		int width = mScreenWidth / (mTabCount > 4 ? 4 : mTabCount);
		mTabs.removeAllViews();
		LayoutParams params = new LayoutParams(width, LayoutParams.MATCH_PARENT);
		for (int i = 0; i < mTabCount; i++) {
			TextView tv = new TextView(this);
			if (i == 0) {
				tv.setText("艺鉴宝设备");
			} else if (i == 1) {
				tv.setText("本地");
			}
			tv.setTextSize(16);
			// tv.setTextColor(this.getResources().getColor(R.color.black));
			tv.setGravity(Gravity.CENTER);
			tv.setTag(i);
			// tv.setBackgroundResource(R.drawable.corners_account_radiogroup_bg);
			tv.setOnClickListener(mOnClickListener);
			mTabs.addView(tv, params);
		}
		setTextColor();
		initCursorView();
	}

	// private int start = 0;
	// private int end = 0;

	private void setTextColor() {
		int len = mTabs.getChildCount();
		int select = mViewPager.getCurrentItem();
		for (int i = 0; i < len; i++) {
			TextView tv = (TextView) mTabs.getChildAt(i);
			if (select == i) {
				tv.setTextColor(getResources().getColor(R.color.text_ju));
			} else {
				tv.setTextColor(getResources().getColor(R.color.black));
			}
		}
		Animation animation = null;
		if (len > 4) {
			int lenght = mTabs.getWidth() - mTabScroll.getWidth();
			int s = (mTabScroll.getWidth() - mOffset) / (len - 1);
			int single = lenght / (len - 1);
			int x = select * single;
			mTabScroll.smoothScrollTo(x, 0);
			System.out
					.println("mCurrIndex=" + mCurrIndex + ",select=" + select);
			System.out
					.println("start=" + s * mCurrIndex + ",end=" + s * select);
			System.out.println("mOffset=" + mOffset);
			System.out.println("s=" + s);

			animation = new TranslateAnimation(s * mCurrIndex, s * select, 0, 0);
			// animation = new TranslateAnimation(single * mCurrIndex, single
			// * select, 0, 0);
		} else {
			animation = new TranslateAnimation(mOffset * mCurrIndex, mOffset
					* select, 0, 0);
		}
		mCurrIndex = select;
		System.out.println("select="+select);
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(200);
		mCursorView.startAnimation(animation);
	}

	private void initCursorView() {
		LayoutParams params = mCursorView.getLayoutParams();
		mOffset = mScreenWidth / (mTabCount > 4 ? 4 : mTabCount);
		// mOffset = (int) (mScreenWidth / 4.0 + 0.5f);
		params.width = mOffset;
		mCursorView.setLayoutParams(params);

		Animation animation = new TranslateAnimation(mOffset * mCurrIndex,
				mOffset * tabSelection, 0, 0);
		mCurrIndex = tabSelection;
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(200);
		mCursorView.startAnimation(animation);
	}

	OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			int item = (Integer) view.getTag();
			// 点击全部刷新
			// mViewPager.setAdapter(mFragmentAdapter);
			mViewPager.setCurrentItem(item);
		}
	};

	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			setTextColor();
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

	public FragmentTransaction mFragmentTransaction;
	public FragmentManager fragmentManager;
	public String curFragmentTag = "";

	/*
	 * 在fragment的管理类中，我们要实现这部操作，而他的主要作用是，当D这个activity回传数据到
	 * 这里碎片管理器下面的fragnment中时，往往会经过这个管理器中的onActivityResult的方法。
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/* 在这里，我们通过碎片管理器中的Tag，就是每个碎片的名称，来获取对应的fragment */
		Fragment f = fragmentManager.findFragmentByTag(curFragmentTag);
		/* 然后在碎片中调用重写的onActivityResult方法 */
		f.onActivityResult(requestCode, resultCode, data);
	}
}
